﻿using System;

namespace ECommerce.Administration
{
	public partial class EditAboutUs : AdminBasePage
	{
		private Service.IProductAdditionalPropertyService _ProductAdditionalPropertyService;

		public Data.Entity.Language CurrentLanguage
		{
			get
			{
				if (SiteContext.Current != null)
				{
					return SiteContext.Current.WorkingLanguage;
				}
				else
				{
					return null;
				}
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				ButtonSave.Text = GetLocalResourceString("Admin.SaveButtonText");
			}
		}

		protected void Page_PreRender(object sender, EventArgs e)
		{
			GetExistContent();
		}

		private void GetExistContent()
		{
			if (CurrentLanguage != null)
			{
				if (_ProductAdditionalPropertyService == null)
				{
					_ProductAdditionalPropertyService = new Service.ProductAdditionalPropertyService();
				}

				var theAboutUsContent = _ProductAdditionalPropertyService.GetTheProductAdditionalPropertyByName("AboutUsContent_" + CurrentLanguage.LanguageCulture);

				if (theAboutUsContent != null)
				{
					EditorAboutUsContent.Content = theAboutUsContent.PropertyValue;
				}
				else
				{
					EditorAboutUsContent.Content = string.Empty;
				}
			}
		}

		protected void ButtonSave_Click(object sender, EventArgs e)
		{
			if (CurrentLanguage != null)
			{
				if (_ProductAdditionalPropertyService == null)
				{
					_ProductAdditionalPropertyService = new Service.ProductAdditionalPropertyService();
				}

				var theAboutUsContent = new Data.Entity.ProductAdditionalProperty();

				theAboutUsContent.DisplayOrder = null;
				theAboutUsContent.ProductID = null;
				theAboutUsContent.PropertyName = "AboutUsContent_" + CurrentLanguage.LanguageCulture;
				theAboutUsContent.PropertyValue = EditorAboutUsContent.Content;

				_ProductAdditionalPropertyService.SaveTheCustomerMustKnownContent(theAboutUsContent);
			}
		}
	}
}
